import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { PersonnelService } from 'src/app/services/personnel.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { PersonnelInfo } from 'src/app/models';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
import { PageTitleService } from 'src/app/services/page-title.service';
interface Person {
  key: string;
  name: string;
  phone: string,
  creationTime: string,
  sex: string,
  department: string,
  status: string;
}
@Component({
  selector: 'zepride-personnel',
  templateUrl: './personnel.component.html',
  styleUrls: ['./personnel.component.less']
})
export class PersonnelComponent implements OnInit {
  listOfData = [];
  dateRange = [];
  visible = false;
  selectedValue = null;
  dateFormat = 'yyyy-MM-dd';
  isConfirmLoading = false;
  isPersonnelVisible = false;
  total = 1;
  loading = true;
  TreeInfo = {};
  UserInfo = {};
  treeList = {};
  objectName = 'User';
  orgTreeId = '';
  id = '';
  presentId = '';
  params = {
    name: '',
    phone: '',
    employeeNo: '',
    gender: '',
    beginTime: '',
    endTime: '',
    pageIndex: 1,
    countOfPage: 10
  };
  personnelInfo: PersonnelInfo = { orgTree: { parentId: '' }, acc: { password: '' } } as PersonnelInfo;
  constructor(
    private route: Router,
    private modal: NzModalService,
    private personnelService: PersonnelService,
    private msg: NzMessageService,
    private phSvr: PageTitleService,
  ) {
    this.phSvr.setPageHeaderOption({
      title: '人员查询',
      subTitle: '用于查询机构管理的人员',
      buttons: [
        {
          text: '添加人员',
          ngType: 'primary',
          icon: "plus-circle",
          onClick: () => {
            this.addEditData(0);
          }
        },
      ]
    });
  }
  ngOnInit(): void {
    this.getData();
  }
  getOrganizationTrees() {

  }
  async getData() {
    this.loading = true;
    try {
      // this.personnelService.orgid = '';
      const data = await this.personnelService.queryUsers(this.params);
      this.listOfData = data.list;
      this.total = data.total;
      this.loading = false;
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  changePageIndex(pageIndex) {
    this.params.pageIndex = pageIndex;
    this.getData();
  }
  changePageSize(pageSize) {
    this.params.countOfPage = pageSize;
    this.getData();
  }
  clickskip(type) {

  }
  close() {
    this.visible = false;
  }
  clear(v, type) {
    if (type == 'name') {
      this.params.name = '';
    } else {
      this.params.phone = '';
    }

    this.onSearch(v, type);
  }
  onSearch(v, type) {
    if (type == 'data') {
      this.params.beginTime = v.length > 0 ? v[0].getTime() : '';
      this.params.endTime = v.length > 0 ? v[1].getTime() : '';
    }
    this.params.pageIndex = 1;
    this.params.gender = this.params.gender ? this.params.gender : '';
    this.getData();
  }
  handleOk(type): void {
    this.isConfirmLoading = true;
    setTimeout(() => {
      this.isPersonnelVisible = false;
      this.isConfirmLoading = false;
    }, 3000);
  }

  handleCancel(type): void {

    this.isPersonnelVisible = false;
  }
  showModal(item) {
    this.personnelInfo = item;
    this.visible = true;
  }
  clickStatus(item) {
    this.modal.confirm({
      nzTitle: `您确定要${item.status == 0 ? '禁用' : '启用'}么?`,
      nzOnOk: () => {
        this.updatePersonnel(item);
      }
    });
  }
  async updatePersonnel(item) {
    item.status = item.status == 0 ? '1' : '0';
    item.orgTree = { objectId: '' };
    item.acc = { id: '' };
    item.orgTree.objectId = item.id;
    item.acc.id = item.accountId;
    try {
      await this.personnelService.updateUser(item);
      this.msg.success(`${item.status == 0 ? '启用' : '禁用'}成功`);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }
  addEditData(Id) {
    this.route.navigate(['/home/editPersonnel'], { queryParams: { Id: Id } });
  }
  delListItem(id, name, i) {
    this.modal.confirm({
      nzTitle: `您确定要删除<b style="color: red;">${name}</b>么?`,
      nzOnOk: () => {
        this.deleteUser(id, i);
      }
    });
  }
  async deleteUser(id, i) {
    try {
      await this.personnelService.deleteUser(id);
      this.listOfData.splice(i, 1);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }


}
